import store from '../index'

export default {
  state: {
    tabs: [{name: '首页',path: '/home'}],
    refresh: true,
    exclude: [],
  },
  mutations: {
    /* tab页添加，超过10自动减1 */
    ADD_TABS:(state,route)=>{
      if(!route.name) return
      let paths = state.tabs.map(val=> val.path);
      if(!paths.includes(route.path)) state.tabs.push(route);
      if(state.tabs.length > 10){
        state.tabs.splice(1,1);
      }
    },
    /* tab页删除 */
    DEL_TAB:(state,path)=>{
      for(let i in state.tabs){
        if(state.tabs[i].path == path){
          state.tabs.splice(i,1);
          break;
        }
      }
    },
    /* 删除其他tab */
    DEL_OTHER_TAB:(state,path)=>{
      let arr = [{name: '首页',path: '/home'}];
      let i = state.tabs.findIndex(item=> item.path == path);
      arr.push(state.tabs[i]);
      state.tabs = arr;
    },
    /* 关闭左侧tab */
    DEL_TO_LEFT:(state,path)=>{
      let arr = [{name: '首页',path: '/home'}];
      let i = state.tabs.findIndex(item=> item.path == path);
      let newArr = state.tabs.slice(i);
      state.tabs = [...arr,...newArr];
    },
    /* 关闭右侧tab */
    DEL_TO_RIGHT:(state,path)=>{
      let i = state.tabs.findIndex(item=> item.path == path);
      let arr = state.tabs.slice(0,i+1);
      state.tabs = [...arr];
    },
    /* tab页删除，全部 */
    DEL_TAB_ALL:(state)=>{
      state.tabs = [{name: '首页',path: '/home'}];
    },
    /* 刷新 */
    SET_REFRESH:(state,val)=>{
      state.refresh = val;
    },
    /* 添加不缓存路由 */
    PUSH_EXCLUDE:(state,name)=>{
      state.exclude.push(name);
    },
    /* 清空排除路由 */
    CLEAR_EXCLUDE:(state)=>{
      state.exclude.length = 0;
    },
  }

}
